﻿using Corina.Datamodel.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class MessageSendService : ServiceBase
    {
        public DataTable GetAllItem(string connectionString, int userId, string content, int index, int size, ref int count)
        {
            List<SqlParameter> paras = new List<SqlParameter>();
            string qstring = "SELECT * FROM ( ";
            string cstring = "SELECT COUNT(*) FROM ( ";
            StringBuilder builder = new StringBuilder();

            builder.Append("SELECT ROW_NUMBER() OVER (ORDER BY tms.SubmitTime) AS ROW_NUMBER, tms.*, tu.Name ");
            builder.Append("  FROM t_message_send AS tms ");
            builder.Append(" LEFT JOIN CORINA_ACCO.dbo.t_user AS tu ON tms.UserId = tu.Id ");
            builder.Append("WHERE 1=1 ");

            #region Conditions
            builder.Append("AND tms.Content LIKE @Content ");
            paras.Add(new SqlParameter("@Content", string.Format("%{0}%", content)));

            if (userId > 0)
            {
                builder.Append("AND tu.Id = @UserId ");
                paras.Add(new SqlParameter("@UserId", userId));
            }

            #endregion

            builder.Append(") AS query ");

            count = DbOperator.GetCount(connectionString, string.Format("{0}{1}", cstring, builder), paras.ToArray());

            builder.Append("WHERE query.[ROW_NUMBER] BETWEEN @Index + 1 AND @Index + @Size ");
            paras.Add(new SqlParameter("@Index", (index - 1) * size));
            paras.Add(new SqlParameter("@Size", size));
            builder.Append("ORDER BY query.[ROW_NUMBER]");

            DataTable table = DbOperator.GetDataTable(connectionString, string.Format("{0}{1}", qstring, builder), paras.ToArray());
            return table;
        }

        public bool Insert(string connectionString, MessageSendInfo entity, params string[] dest)
        {
            if (dest.Length > 0)
            {
                List<string> commands = new List<string>();
                List<SqlParameter> paras = new List<SqlParameter>();
                commands.Add("INSERT INTO t_message_send(Guid,UserId,[Content],SubmitTime,SendWay) VALUES(@Guid,@UserId,@Content,@Time,@Way) ");
                paras.Add(new SqlParameter("@Guid", entity.Guid));
                paras.Add(new SqlParameter("@UserId", entity.UserId));
                paras.Add(new SqlParameter("@Content", entity.Content));
                paras.Add(new SqlParameter("@Time", entity.SubmitTime));
                paras.Add(new SqlParameter("@Way", (int)entity.SendWay));

                for (var i = 0; i < dest.Length;i++ )
                {
                    commands.Add(string.Format("INSERT INTO t_message_dest(Guid,Dest) VALUES(@Guid,@Dest_{0}) ", i));
                    paras.Add(new SqlParameter("@Dest_" + i, dest[i]));
                }
                    //dest.ToList().ForEach(t =>
                    //{
                    //    commands.Add("INSERT INTO t_message_dest(Guid,Dest) VALUES(@Guid,@Dest) ");
                    //    //commands.Add(new MessageDestInfo() { Guid = entity.Guid, Dest = t }.SqlInsertString);
                    //});

                    return DbOperator.ExexuteCommand(connectionString, commands, paras.ToArray()) > 0;
            }
            else
            {
                return false;
            }
        }

        public bool Remove(string connectionString, int id)
        {
            return DbOperator.RemoveEntityById<MessageSendInfo>(connectionString, id);
        }

    }
}
